<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * 来话导航控制器
 *
 * @author Alen
 * @since 1.0 2014-6-19 by sutroon
 */
class IncomingCallController extends AppbaseController {

    public function rule_list() {
        $this->display();
    }

    /**
     * 获取列表查询数据
     * @return array
     * @since 1.0 <2015-7-2> SoChishun Added.
     */
    function get_rule_search_data() {
        $search = I();
        if (!$this->isSuperAdmin) {
            $where['siteID'] = $this->loginData['siteID'];
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_rule_list_json() {
        $search_data = $this->get_rule_search_data();
        $list = AddonD('IncomingCall')->get_rule_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function rule_export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $search_data = $this->get_rule_search_data();
        $list = AddonD('IncomingCall')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function rule_edit($id = 0) {
        if ($id) {
            $data = M('tcti_income_nav')->find($id);
            $this->assign('data', $data);
        }
        $this->display();
    }

    public function rule_save() {
        $callerNo = I('callerNo');
        $calledNo = I('calledNo');
        $dateType = I('dateType');
        $params = new \Org\Su\Params();
        $params->addParam('siteID', 'number', '租户未选择!');
        $params->addParam('name', 'require', '导航名称必须填写!');
        $params->addParam('trunkName', 'require', '中继名称必须填写!');
        $params->addParam('regMode', 'require', '中继注册模式无效!');
        $params->addParam('recState', 'number', '录音标识必须选择!');
        if ($dateType == '2') {
            $params->addData('beginTime', '00:00:00');
            $params->addData('endTime', '23:59:59');
        } else {
            $params->addParam('beginTime', 'require', '开始时间必须选择!');
            $params->addParam('endTime', 'require', '结束时间必须选择!');
        }
        $params->addParam('dateType', 'number', '日期类型必须选择!');
        $params->addParam('outOper', 'require', '呼通后操作必须填写!');
        $outOper = I('outOper');
        if (strpos(',1,6,2,3,4,5,', ",$outOper,") !== false) {
            $params->addParam('music', 'require', '放音文件必须选择!');
        } else {
            $params->addData('music', '');
        }
        if (strpos(',1,2,3,4,5,', ",$outOper,") !== false) {
            $params->addParam('musicLimit', 'number', '最大放音次数必须填写!');
        } else {
            $params->addData('musicLimit', '');
        }
        if (strpos(',1,2,', ",$outOper,") !== false) {
            $params->addParam('outParam', 'require', '呼通后操作具体参数必须填写!');
        } else {
            $params->addData('outParam', '');
        }
        if (strpos(',0,6,', ",$outOper,") !== false) {
            $params->addParam('outQueue', 'require', '呼通后操作队列号必须选择!');
        } else {
            $params->addData('outQueue', '');
        }
        if (strpos(',0,1,6,', ",$outOper,") !== false) {
            $outEcEnable = I('outEcEnable');
            $params->addParam('sayExtension', 'number', '播放工号标识必须选择!');
        } else {
            $outEcEnable = 0;
            $params->addData('sayExtension', '0');
        }
        $params->addData('outEcEnable', $outEcEnable);
        if ($outEcEnable == '1') {
            $params->addParam('outEcMusic', 'require', '评价放音文件必须选择!');
            //$params->addParam('outEcKey', 'require', '评价按键必须填写!');
        } else {
            $params->addData('outEcMusic', '');
            //$params->addData('outEcKey', '');
        }
        if (!$callerNo && !$calledNo) {
            $params->addError('主叫号码或被叫号码至少填写一个!');
        }
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $params->addData('callerNo', $callerNo);
        $params->addData('calledNo', $calledNo);
        $id = I('id');
        $M = M('tcti_income_nav');
        if ($id) {
            $where['id'] = $id;
            $result = $M->where($where)->save($params->getParams());
        } else {
            // 检测名称是否重复
            $name = I('name');
            $where['name'] = $name;
            if ($M->where($where)->count() > 0) {
                $this->ajaxMsg(false, '[' . $name . '] 已存在!');
            }
            $params->addData('createdTime', date('Y-m-d H:i:s'));
            $result = $M->add($params->getParams());
            $id = $result;
        }
        if ('*' == $callerNo) {
            $msg = exec("su asterisk -rx 'module unload cdr_mysql.so'", $a, $i);
            if ($i) {
                $this->ajaxMsg(false, '命令执行失败!');
            }
        }
        $this->ajaxMsg($result, '保存{%}!');
    }

    /**
     * 删除项目
     */
    public function rule_delete() {
        $this->itemDelete('tcti_income_nav');
    }

    public function black_list() {
        $this->display();
    }

    /**
     * 获取列表查询数据
     * @return array
     * @since 1.0 <2015-7-2> SoChishun Added.
     */
    function get_black_search_data() {
        $search = I();
        if (!$this->isSuperAdmin) {
            $where['siteID'] = $this->loginData['siteID'];
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_black_list_json() {
        $search_data = $this->get_black_search_data();
        $list = AddonD('BlackList')->get_blacknumber_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function black_export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $search_data = $this->get_black_search_data();
        $list = AddonD('BlackList')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function black_edit() {
        $this->itemEdit('tcti_number_limit');
    }

    public function black_save() {
        $params = new \Org\Su\Params();
        if ($this->isSuperAdmin) {
            $params->addParam('siteID', 'number', '租户未选择!');
        } else {
            $params->addData('siteID', $this->loginData['siteID']);
        }
        $params->addParam('phoneNumber', 'require', '电话号码必须选择!');
        $params->addParam('state', 'number', '状态必须选择!');
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $id = I('id');
        $num = I('phoneNumber');
        $M = M('tcti_number_limit');
        if ($id) {
            $where['id'] = $id;
            $result = $M->where($where)->save($params->getParams());
        } else {
            if ($M->where('phoneNumber="' . $num . '" and siteID=' . $this->loginData['siteID'])->count() > 0) {
                $this->ajaxMsg(false, '号码 [' . $num . '] 已添加过了!');
            }
            $params->addData('createdTime', date('Y-m-d H:i:s'));
            $result = $M->add($params->getParams());
        }
        if ($result !== false) {
            $astman = create_asmanager();
            $astman->database_put("blacklist", $num, '1');
            $astman->database_del("blacklist", "blocked");
        }
        $this->ajaxMsg($result, '保存{%}!');
    }

    /**
     * 删除项目
     */
    public function black_delete($id = '') {
        if (!$id) {
            $this->ajaxMsg(false, '编号丢失!');
        }
        $M = M('tcti_number_limit');
        $numbers = $M->where(array('id' => array('in', $id)))->getField('phoneNumber', true);
        $result = $M->delete($id);
        if ($result !== false) {
            $astman = create_asmanager();
            foreach ($numbers as $number) {
                $astman->database_del("blacklist", $number);
            }
        }
        $this->ajaxMsg($result, '删除{%}!');
    }

}
